package com.abel.roommanager.service.imp;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.abel.roommanager.mapper.ClazzMapper;
import com.abel.roommanager.mapper.StudentMapper;
import com.abel.roommanager.pojo.Clazz;
import com.abel.roommanager.service.ClazzService;
import com.abel.roommanager.util.Page;
import lombok.Data;
@Service
@Data
public class ClazzServiceImp implements ClazzService{
	@Resource
    private ClazzMapper clazzMapper;
	@Resource
	private StudentMapper studentMapper;

	@Override
	public Page<Clazz> findAllClasses(int currentPage) {
		int count=3;
		Page<Clazz> page=new Page<>();
		int index=(currentPage-1)*count;
		List<Clazz> clazzs=clazzMapper.findAllClasses(index,count);
//		查询班级有多少
		int total=clazzMapper.findTotalClasses();
		page.setIndex(index);
		page.setTotal(total);
		page.setData(clazzs);
		page.setCurrentPage(currentPage);
		page.setCount(count);
		page.setNextPage(currentPage+1);
		page.setPrevPage(currentPage-1);
		int totalSize=(total%count==0)?(total/count):(total/count+1);
		page.setTotalSize(totalSize);
		page.setUrl("cla/findAll");
		return page;
	}

	@Override
	public int addClass(String cname, String time, int tid) {
		
		return clazzMapper.addClass(cname,time,tid);
	}

	@Override
	public int delClassByCid(int cid) {
//  查询该班下有无学生，有学生不能删除
		int re=0;
		int count=studentMapper.findStudentsCountByCid(cid);
		if (count>0) {
			re=1;
		}else {
//			删除班级信息
			int result=clazzMapper.delClassByCid(cid);
			if (result>0) {
				re=2;//删除成功;
			}
		}
		return re;
	}

	@Override
	public List<Clazz> findClasses() {
		return clazzMapper.findClasses();
	}
}
